package aip.assignment2.service;

import aip.assignment2.persistence.entity.Product;
import aip.assignment2.service.entity.ProductInCart;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 * This class contains shopping cart management functions.
 */
@Stateful(name = "aip.assignment2.service.ShoppingCart")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ShoppingCart implements Serializable
{
	public ShoppingCart()
	{
		productsInCart = new LinkedList<ProductInCart>();
	}
	/**
	 * the current products in the shopping cart
	 */
	private List<ProductInCart> productsInCart;
	/**
	 * the current products in the shopping cart
	 * @return the productsInCart
	 */
	public List<ProductInCart> getProductsInCart()
	{
		return productsInCart;
	}
	/**
	 * the total price of all products
	 * @return the totalPrice
	 */
	public BigDecimal getTotalPrice()
	{
		BigDecimal result = BigDecimal.ZERO;
		for (ProductInCart productInCart : productsInCart)
		{
			result = result.add(productInCart.getPrice());
		}
		return result;
	}
	/**
	 * This method adds a kind of products.
	 * @param product the product to be added
	 * @param quantity the quantity of the products to be added
	 * @return whether the method succeeds
	 */
	public boolean add(Product product, int quantity)
	{
		if (product == null || quantity <= 0)
		{
			return false;
		}
		for (int i = 0; i < productsInCart.size(); i++)
		{
			ProductInCart productInCart = productsInCart.get(i);
			if (productInCart.getProduct().equals(product))
			{
				productInCart.setQuantity(productInCart.getQuantity() + quantity);
				return true;
			}
		}
		ProductInCart productInCart = new ProductInCart(product, quantity);
		productsInCart.add(productInCart);
		return true;
	}
	/**
	 * This method updates an existing product's quantity.
	 * @param product the product in the shopping cart
	 * @param quantity the quantity of the product to be updated to
	 * @return whether the method succeeds
	 */
	public boolean update(Product product, int quantity)
	{
		if (product == null || quantity <= 0)
		{
			return false;
		}
		for (int i = 0; i < productsInCart.size(); i++)
		{
			ProductInCart productInCart = productsInCart.get(i);
			if (productInCart.getProduct().equals(product))
			{
				productInCart.setQuantity(quantity);
				return true;
			}
		}
		return false;
	}
	/**
	 * This method removes an existing product.
	 * @param product the product in the shopping cart to be removed
	 * @return whether the method succeeds
	 */
	public boolean remove(Product product)
	{
		if (product == null)
		{
			return false;
		}
		for (int i = 0; i < productsInCart.size(); i++)
		{
			ProductInCart productInCart = productsInCart.get(i);
			if (productInCart.getProduct().equals(product))
			{
				productsInCart.remove(i);
				return true;
			}
		}
		return false;
	}
	/**
	 * This method clears all products.
	 */
	public void clear()
	{
		productsInCart.clear();
	}
	/**
	 * This method is the destructor of the shopping cart.
	 */
	@Remove
	public void dispose()
	{
	}
}
